package com.linghang.back.dto;

import com.linghang.back.entity.Article;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 文章数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ArticleDTO {
    
    private Long id;
    private String title;
    private String author;
    private String digest;
    private String coverUrl;
    private String type;
    private Integer readCount;
    private String publishDate;
    private String content;
    private String wechatArticleId;
    private String wechatUrl;
    private Boolean showCoverPic;
    private Boolean needOpenComment;
    private Boolean onlyFansCanComment;
    private Boolean isDeleted;
    
    /**
     * 将实体转换为DTO
     */
    public static ArticleDTO fromEntity(Article article) {
        ArticleDTO dto = new ArticleDTO();
        dto.setId(article.getId());
        dto.setTitle(article.getTitle());
        dto.setAuthor(article.getAuthor());
        dto.setDigest(article.getDigest());
        dto.setCoverUrl(article.getCoverUrl());
        dto.setType(article.getType());
        dto.setReadCount(article.getReadCount());
        dto.setWechatArticleId(article.getWechatArticleId());
        dto.setWechatUrl(article.getWechatUrl());
        dto.setShowCoverPic(article.getShowCoverPic());
        dto.setNeedOpenComment(article.getNeedOpenComment());
        dto.setOnlyFansCanComment(article.getOnlyFansCanComment());
        dto.setIsDeleted(article.getIsDeleted());
        
        // 格式化日期为字符串
        if (article.getPublishDate() != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            dto.setPublishDate(article.getPublishDate().format(formatter));
        }
        
        // 文章详情页时才需要内容
        dto.setContent(article.getContent());
        
        return dto;
    }
    
    /**
     * 列表展示DTO，不包含内容字段
     */
    public static ArticleDTO fromEntityForList(Article article) {
        ArticleDTO dto = fromEntity(article);
        dto.setContent(null); // 列表中不需要显示内容
        return dto;
    }
} 